package com.csu.express.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csu.common.exception.WebException;
import com.csu.common.utils.PasswdUtil;
import com.csu.express.constants.ErrorCode;
import com.csu.express.dao.CustomerMapper;
import com.csu.express.pojo.CustomerPojo;
import com.csu.express.pojo.OrderPojo;
import com.csu.express.service.CustomerService;
import com.csu.express.vo.CustomerVo;

/**
 * @项目名称：express
 * @类名称：UserServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月21日 下午3:55:47
 * @company:
 */
@Service
public class CustomerServiceImpl implements CustomerService
{

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * @see com.csu.express.service.CustomerService#sign(java.lang.String,
     *      java.lang.String, java.lang.String)
     */
    @Override
    public CustomerPojo sign(String cellphone, String password, String name)
    {
        String passwordMd5 = PasswdUtil.md5Passwd(password);
        CustomerPojo customer = new CustomerPojo();
        customer.setName(name);
        customer.setCellPhone(cellphone);
        customer.setPasswd(passwordMd5);
        customer.setCreateTime(new Date());
        customerMapper.insert(customer);
        return customer;
    }

    /**
     * @throws WebException
     * @see com.csu.express.service.CustomerService#login(java.lang.String,
     *      java.lang.String)
     */
    @Override
    public CustomerPojo login(String cellPhone, String password) throws WebException
    {
        CustomerPojo customer = customerMapper.selectByCellPhone(cellPhone);

        if (customer == null)
        {
            throw new WebException(ErrorCode.UserNullCode, ErrorCode.UserNullMessgae);
        }

        if (!PasswdUtil.checkPasswd(password, customer.getPasswd()))
        {
            throw new WebException(ErrorCode.PasswdErrorCode, ErrorCode.PasswdErrorMessgae);
        }

        return customer;
    }

    /**
     * @see com.csu.express.service.CustomerService#getOrders()
     */
    @Override
    public List<OrderPojo> getOrders()
    {
        // TODO Auto-generated method stub
        List<OrderPojo> orders = new ArrayList<OrderPojo>();
        for (int i = 0; i < 10; i++)
        {
            OrderPojo order = new OrderPojo();
            order.setId(i);
            order.setCustomerId(1);
            order.setDriverId(1);
            order.setLinkman("李四");
            order.setLinknumber("13560344027");
            order.setSourceAddress("北京市西城区");
            order.setStatus(1);
            order.setTargetAddress("北京市海淀区");
            order.setServiceType(1);
            orders.add(order);
        }
        return orders;

    }

    @Override
    public CustomerVo getCustomerInfoById(Integer customerId)
    {
        return customerMapper.getCustomerInfoById(customerId);
    }
}
